<template>
  <e-table
    ref="eTable"
    @getList="handleGetList"
    :tableData="tableData"
    :columns="columns"
    :loading="loading"
    :pageObj="null"
  >
    <el-table-column slot="action" fixed="right" width="50" align="center">
      <template slot-scope="scope">
        <el-dropdown>
          <a class="el-dropdown-link">
            <i class="el-icon-menu"></i>
          </a>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <a @click="handleEdit(true, scope.row)">编辑</a>
            </el-dropdown-item>
            <el-dropdown-item>
              <a class="red" @click="handleDelete(scope.row)">删除</a>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
    <el-table-column label="项目类型" width="140" align="center" slot="type">
      <template slot-scope="scope">{{ computeType(scope.row.internalItemType) }}</template>
    </el-table-column>
    <el-table-column label="项目分类" width="150" align="center" slot="typeName">
      <template slot-scope="scope">
        <span>{{ scope.row.internalTypeName }}</span>
      </template>
    </el-table-column>
    <el-table-column label="项目名称" align="center" slot="itemName">
      <template slot-scope="scope">
        <span class="fl">{{ scope.row.internalItemName }}</span>
      </template>
    </el-table-column>
    <!-- <el-table-column label="是否末级" width="80" align="center" slot="leaf">
      <template slot-scope="scope">{{ scope.row.leaf ? '是' : '否' }}</template>
    </el-table-column>-->
    <el-table-column label="排序" width="100" align="center" slot="sort">
      <template slot-scope="scope">
        <el-input @blur="handleOrder(scope.row)" v-model.number="scope.row.itemOrder" />
      </template>
    </el-table-column>
  </e-table>
</template>

<script>
import { definePage, defineDelete, defineUpdate } from '@/api/satisfaction'

import { openNotification } from '@/utils/util'
export default {
  data() {
    return {
      tableData: [],
      loading: false,
      queryParam: {
        pageSize: 10,
        pageNo: 1
      },
      columns: [{ slot: 'typeName' }, { slot: 'type' }, { slot: 'itemName' }, { slot: 'leaf' }, { slot: 'sort' }, { slot: 'action' }]
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    computeType(num) {
      let str = ''
      switch (num) {
        case 1:
          str = '科间互评'
          break
        case 2:
          str = '科内互评'
          break
        case 3:
          str = '评上级领导'
          break
        case 4:
          str = '评院领导'
          break
        default:
          break
      }
      return str
    },
    async handleOrder(row) {
      if (row.itemOrder > 100 || row.itemOrder < 0) {
        this.$message.error('排序值限制0-100')
        return false
      }
      if (Object.is(row.itemOrder, NaN)) {
        this.$message.error('您输入不是一个数字')
        return false
      }
      const res = await defineUpdate(row)
      if (res.state === '000000') {
        this.handleGetList()
      }
    },
    handleEdit(bool, obj) {
      this.$emit('edit', bool, obj)
    },
    handleData(arr1, arr2) {
      arr1.map(v => {
        v.children = []
        arr2.map(j => {
          if (v.internalItemId === j.parentItemId) {
            v.children.push(j)
            if (v.children && v.children.length > 0) {
              this.handleData(v.children, arr2)
            }
          }
        })
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await definePage(this.queryParam)
      if (res && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
      } else {
        this.tableData = []
      }
      this.loading = false
    },
    handleDelete(row) {
      console.log('row', row)
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.internalItemName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await defineDelete(row.internalItemId, {})
          openNotification('success', '删除成功')
          const index = _this.tableData.findIndex(v => v.internalItemId === row.internalItemId)
          _this.tableData.splice(index, 1)
        }
      })
    }
  }
}
</script>
